/**
 * Created by liwei on 17/5/25.
 */

'use strict'
import Vue from 'vue'

var exportTemplete = require('html!./input_set.html')
require('./input.less')

module.exports = {
  template: exportTemplete,
  data: function () {
    return {
      dataList: [],
      fileHead: [],
      fileList: [],
      userId: localStorage.getItem('userId'),
      modelId: '',
      dataHead: {},
      modelName: '',
      modelPic: '',
      oldTableName:"",
      type:''//值为add代表新增，其他的值就代表编辑
    }
  },
  created: function () {
    this.modelId = this.$route.query.modelId;
    this.modelPic = this.$route.query.modelPic;
    this.modelName = this.$route.query.modelName;
    this.type = this.$route.query.type;
    this.getAllData()
    this.init()
  },
  methods: {
    init: function () {
      $(document).on('click', '.icon-dot', function (e) {
        $(this).addClass('active').parent().siblings().find('.icon-dot').removeClass('active')
      })
    },
    getAllData: function () {
      var me = this
      me.$http.get('/algorithmController/inputSetTSData.do?modelId=' + me.modelId)
        .then(function (res) {
          me.dataList = []
          if (res.data.code == '000000') {
            console.log(res)
            for (var j = 0; j < res.data.result.length; j++) {
              me.dataList.push(res.data.result[j])
            }
            me.dataHead = res.data.result[res.data.result.length - 1]
          } else {
            layer.msg('系统繁忙，请稍后再试')
          }

        }.bind(this))
    },
    /**
     * 获取浏览器url参数
     * @param  {String} name 参数值
     *
     */
    getQueryString: function (name) {
      var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i')
      var r = window.location.search.substr(1).match(reg)
      if (r != null) return unescape(r[2])
      return null
    },
    /**
     * 添加算法
     */
    add: function (AIId, AIname) {
      var temp = '<div class="selected-item">' +
        '        <span class="pl30">' + AIname + '</span>' +
        '        <span class="del fr ml10 delete">删除</span>' +
        '        <input type="text" style="display: none" value="' + AIId + '">删除</input>' +
        '        </div>'
      $('#cleckAi').append(temp)
    },
    goBack:function(){
      var me = this;
      me.$router.push({path: '/product/modelDataExplore', query: {modelId: me.modelId}})
    },
    /**
     * 下一步
     */
    submit: function () {
      var layLoad = layer.load(2, {shade: 0.1, area: ['32px', '64px']})
      var me = this
      var dataTr = $('#data').find('tr')
      var K2Val = [];
      for (var i = 0; i < dataTr.length; i++) {

        var select = $(dataTr[i]).find('.active')
        var id = $(select).attr('value')
        var val = $(select).parent('label').attr('value')
        console.log(id);
        var tmep =
          {
            id: id,
            value: val
          }
        K2Val.push(tmep);
      }
      console.log(K2Val);

      $.ajax({
        url: Vue.prototype.$http.defaults.baseURL + '/algorithmController/doInputSetTSData.do',
        method: 'post',
        data: {
          'modelId': me.modelId,
          'colIds': JSON.stringify(K2Val)
        },
        dataType: 'json',
        success: function (res) {
          layer.close(layLoad)

          console.log(res);
          if (res.code == '000000') {
            localStorage.setItem('modelId', me.modelId);
            me.oldTableName=res.result;
            localStorage.setItem('oldTableName', me.oldTableName);
            me.$router.push('/product/show-setting');
            //me.getDataList()//执行算法
          } else {
            layer.msg(res.message);
          }
        },
        error: function (error) {
          console.error(error, 'error')
        }
      })
    }
  }
}
